package cn.qqy.base.service.impl;

import cn.qqy.auth.domain.Permission;
import cn.qqy.auth.mapper.PermissionMapper;
import cn.qqy.base.annotation.RonghuanetPermission;
import cn.qqy.base.service.IAutoScanPermissionService;
import cn.qqy.base.utils.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Service
public class AutoScanPermissionService implements IAutoScanPermissionService {

    @Value("${scan.paths}")
    private String paths;
    @Value("${scan.tages}")
    private Boolean tages;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void scan() {
        List<Class> allClassName = ClassUtils.getAllClassName(paths);
//        System.out.println(allClassName);
        List<Permission> permissions = new ArrayList<>();
        for (Class aClass : allClassName) {//循环扫描的controller
            //获取@RonghuanetPermission的内容
            RonghuanetPermission ronghuanet = (RonghuanetPermission) aClass.getAnnotation(RonghuanetPermission.class);
            if (ronghuanet==null)
                continue;
            Permission parent = new Permission();
            parent.setName(ronghuanet.name());
            parent.setDescs(ronghuanet.desc());
            RequestMapping requestMapping = (RequestMapping) aClass.getAnnotation(RequestMapping.class);
            parent.setUrl(requestMapping.value()[0]);
            parent.setSn(aClass.getSimpleName());

            permissions.add(parent);
            Permission permission = permissionMapper.queryBySn(parent.getSn());
            if (permission != null){
                parent.setId(permission.getId());
                permissionMapper.update(parent);
            }else {
                permissionMapper.save(parent);
            }



            Method[] methods = aClass.getMethods();
            for (Method method : methods) {
                RonghuanetPermission annotation = method.getAnnotation(RonghuanetPermission.class);
                if (annotation==null) continue;
                Permission child = new Permission();
                child.setName(annotation.name());
                child.setDescs(annotation.desc());

                child.setSn(parent.getSn()+":"+method.getName());
                child.setParentId(parent.getId());
                String url = getchildUrl(method);
                child.setUrl(parent.getUrl()+url);
                Permission childles = permissionMapper.queryBySn(child.getSn());
                if (childles != null){
                    child.setId(childles.getId());
                    permissionMapper.update(child);
                }else {

                    permissionMapper.save(child);
                }


            }


        }

    }

    private String getchildUrl (Method method){
        RequestMapping annontation = method.getAnnotation(RequestMapping.class);
        if (annontation!=null){
            return annontation.value().length>0?annontation.value()[0]:"";
        }
        GetMapping getMapping = method.getAnnotation(GetMapping.class);
        if (getMapping!=null){
            return getMapping.value().length>0?getMapping.value()[0]:"";
        }
        PutMapping putMapping = method.getAnnotation(PutMapping.class);
        if (putMapping!=null){
            return putMapping.value().length>0?putMapping.value()[0]:"";
        }
        DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
        if (deleteMapping!=null){
            return deleteMapping.value().length>0?deleteMapping.value()[0]:"";
        }
        PostMapping postMapping = method.getAnnotation(PostMapping.class);
        if (postMapping!=null){
            return postMapping.value().length>0?postMapping.value()[0]:"";
        }
        PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
        if (patchMapping!=null){
            return patchMapping.value().length>0?patchMapping.value()[0]:"";
        }
        return "";
    }

}
